<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="isEditRef ? modalConfig.header.updateTitle : modalConfig.header.createTitle"
      width="30%"
      center
    >
      <div class="form">
        <el-form :model="formData" label-width="80px" size="large">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input v-model="formData[item.prop]" :placeholder="item.placeholder"></el-input>
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="formData[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select v-model="formData[item.prop]" :placeholder="item.placeholder">
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value"></el-option>
                  </template>
                </el-select>
              </template>
              <template v-if="item.type === 'custom'">
                <slot :name="item.slotName"></slot>
              </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmitBtnClick">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import useSystemStore from '@/store/main/system/system'
import { reactive, ref } from 'vue'
import type { IProps } from './type'

defineOptions({
  name: 'PageModal'
})

const props = defineProps<IProps>()
const emit = defineEmits(['ok'])

const dialogVisible = ref(false)

const initialData: any = {
  id: undefined
}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = undefined
}

const formData = reactive<any>(initialData)

// 当前页面是否为编辑页面
const isEditRef = ref(false)

function setModalVisible(isEdit: boolean, itemData?: any) {
  dialogVisible.value = true
  isEditRef.value = isEdit

  // 编辑数据
  if (isEdit && itemData) {
    for (const key in formData) {
      formData[key] = itemData[key]
    }
  } else {
    // 新建数据
    for (const key in formData) {
      formData[key] = undefined
    }
  }
}

const systemStore = useSystemStore()
// 确定提交
async function handleSubmitBtnClick() {
  let infoData = formData
  // 额外参数
  if (props.otherInfo) {
    infoData = { ...formData, ...props.otherInfo }
  }

  if (isEditRef.value) {
    // 编辑用户
    await systemStore.updatePageAction(props.modalConfig.pageName, formData.id, infoData)
  } else {
    // 创建用户
    await systemStore.createPageAction(props.modalConfig.pageName, infoData)
  }

  emit('ok')
  dialogVisible.value = false
}

defineExpose({
  setModalVisible
})
</script>

<style lang="less" scoped>
.form {
  padding: 0 20px;
}
</style>
